
//
//  ByUserIndexBranchHospitalVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/14.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserIndexBranchHospitalVC.h"

// cell
#import "ByCellUserIndexBranch.h"

// view
#import "ByViewUserIndexBranchLocation.h"
#import "ByCoreToolLocation.h"

// api
#import "ByApiUserIndex.h"

@interface ByUserIndexBranchHospitalVC ()<UITableViewDelegate,UITableViewDataSource,ByCoreLoactionDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByViewUserIndexBranchLocation *locationView;

@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, copy)   NSString *lat;
@property (nonatomic, copy)   NSString *lng;

@end

@implementation ByUserIndexBranchHospitalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"分院选择";
    [self addLeftBackBarButtonItem];
    self.view.backgroundColor = BY_TABLE_COLOR;
    [self fetchData];
    [self createViews];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self location];
}

#pragma mark - getter
- (NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

#pragma mark - fetch data
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中..."];
    [ByApiUserIndex getBranchList:model success:^(ByNetBaseResult *result) {
//        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for(ByModelIndexBranch *model in result.data[@"list"]) {
            [temp addObject:model];
        }
//        if (self.page_index == 1) {
            [self.dataAry removeAllObjects];
//            [self.dataAry addObjectsFromArray:[temp copy]];
//            [self.tableView.mj_footer resetNoMoreData];
//        }else{
            [self.dataAry addObjectsFromArray:[temp copy]];
//        }
        [self.tableView reloadData];
        [ByCoreToolAlert hideHud];
//        if (self.dataAry.count == count) {
//            [self.tableView.mj_header endRefreshing];
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//            return ;
//        }
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}

#pragma mark - create views
- (void)createViews{
    self.locationView = [[ByViewUserIndexBranchLocation alloc] init];
    [self.view addSubview:self.locationView];
    [self.locationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(15);
        }else{
            make.top.equalTo(self.view.mas_top).offset(15);
        }
    }];
    self.locationView.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(self.locationView.mas_bottom).offset(15);
        make.right.equalTo(@(0));
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellUserIndexBranch *cell = [ByCellUserIndexBranch cellWithTableView:tableView];
    cell.model = self.dataAry[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectBranchBlcok) {
        self.selectBranchBlcok(self.dataAry[indexPath.section]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 定位ck
- (void)location {
    
    [ByCoreToolAlert showLoadind:@"正在定位..."];
    ByCoreToolLocation *locationManager = [ByCoreToolLocation shareInstance];
    locationManager.delegate = self;
    [locationManager configLocationService];
    [locationManager startUpdateLoaction];
}
- (void)locationDidComplete:(NSDictionary *)placeInfoDic {
    
    [ByCoreToolAlert hideHud];
    // 设置定位城市
    self.locationView.address = [NSString stringWithFormat:@"%@%@",placeInfoDic[@"SubLocality"],placeInfoDic[@"City"]];

    
}
- (void)locationDidFail:(NSError *)error {
    [ByCoreToolAlert showOnlyText:@"定位失败"];
}



@end
